package com.guanyong.services;

import com.guanyong.constant.VideoConstant;
import com.guanyong.dao.IAdvertisementDaoMapper;
import com.guanyong.model.AdvertisementModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * Created by mxk on 2017/5/31.
 * 价格业务
 */
@Service
public class AdService {
    @Autowired
    IAdvertisementDaoMapper iAdvertisementDaoMapper;




    public AdvertisementModel GetAdByid(Integer id){

        return  iAdvertisementDaoMapper.GetAdvertisementModelById(id);
    }

    public Integer UpdateAd(AdvertisementModel advertisementModel){
        return  iAdvertisementDaoMapper.update(advertisementModel);
    }


    public Integer AddAd(AdvertisementModel bannerListModel){
        return  iAdvertisementDaoMapper.insert(bannerListModel);
    }

    public Integer DleteAd(Integer id){
        return  iAdvertisementDaoMapper.delete(id);
    }


    //
    public HashMap<String,Object> GetAdList(
            Integer type,
            Integer pno,
            Integer pagesize,
            Integer allcount){

        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iAdvertisementDaoMapper
                    .SelectAdListCount(type);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<AdvertisementModel> advertisementModels
                =iAdvertisementDaoMapper.SelectAdList(type,(pno-1)*pagesize,pagesize);

        hashMap.put("list",advertisementModels);
        return hashMap;
    }

    public AdvertisementModel GetAdRom(Integer type){
        Integer count=iAdvertisementDaoMapper
                .SelectAdListCount(type);
        Random rand = new Random();
        int i = rand.nextInt(count);
        List<AdvertisementModel> list
                =iAdvertisementDaoMapper.SelectAdList(type,i,1);
        return list.get(0);
    }
}
